﻿using System;
using System.Collections.Specialized;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Com.FastSearch.Esp.Search;
using Com.FastSearch.Esp.Search.Query;
using Com.FastSearch.Esp.Search.View;
using Com.FastSearch.SharePoint.BL.SearchManager;
using Com.FastSearch.SharePoint.Interfaces;
using Com.FastSearch.SharePoint.WebControls.ESPSearchWebParts.UI;
using Com.FastSearch.SharePoint.WebControls.ESPSearchWebParts.UI.Client;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace ESPWebPartsUnitTest
{
    /// <summary>
    /// Summary description for ESPSearchManagerTest
    /// </summary>
    [TestClass]
    public class ESPSearchManagerTest
    {
        public ESPSearchManagerTest()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void TestGetSearchViews()
        {
            IESPSearchManager manager = GetSearchManager();
            StringCollection views = manager.GetSearchViews();
            Assert.IsTrue(views.Count == 3);            
        }


        [TestMethod]
        public void TestGetSortableFieldsAndRankProfiles()
        {
            IESPSearchManager manager = GetSearchManager();
            List<ISortField> sort = manager.GetSortableFieldsAndRankProfiles("espsystemwebcluster");
            Assert.IsTrue(sort.Count == 4);
        }

        [TestMethod]
        public void TestAddQueryParameter()
        {
            //ESPSearchManager_Accessor manager = ESPSearchManager_Accessor.GetInstance(CreateSearchFactory(), "MockFactory");
            IQuery query = new Query("test");
            ESPSearchManager_Accessor.AddSearchParameters(query, GetMockSearchRequest());          
            Assert.AreEqual("true", query.GetStringParameter(BaseParameter.LEMMATIZE));
            Assert.AreEqual("auto", query.GetStringParameter(BaseParameter.SPELL));
            Assert.AreEqual("true", query.GetStringParameter(ESPSearchManager.QTF_SYNONYM_PARAM));
            Assert.AreEqual("b", query.GetStringParameter("a"));
            Assert.AreEqual(0, query.GetIntParameter(BaseParameter.OFFSET));
            Assert.AreEqual(10, query.GetIntParameter(BaseParameter.HITS));                      
        }

        [TestMethod]
        public void TestBuildQueryStringFromTemplate()
        {
            //ESPSearchManager_Accessor manager = ESPSearchManager_Accessor.GetInstance(CreateSearchFactory(), "MockFactory");
            string template1 = "test {0} {1}";
            string resu = new FQLQueryBuilder().BuildQueryStringFromTemplate("a" , template1);
            Assert.AreEqual("test a user", resu);

            // Test with wrong formatted template.
            string template2 = "test {0 {1}";
            resu = "";
            bool failed = false;
            try
            {
                resu = new FQLQueryBuilder().BuildQueryStringFromTemplate("a", template2);    
            } catch(FormatException)
            {
                failed = true;
            }
            Assert.IsTrue(failed);            
        }

         [TestMethod]
        public void TestGetSearchfactoryConfigFromString()
        {
            //ESPSearchManager_Accessor manager = ESPSearchManager_Accessor.GetInstance(CreateSearchFactory(), "MockFactory");            
            NameValueCollection resu = ESPSearchManager_Accessor.GetSearchfactoryConfigFromString("localhost:15100");
            Assert.AreEqual("localhost:15100", resu["Com.FastSearch.Esp.Search.Http.QRServers"]);
        }


        

        [TestMethod]
        public void TestSearch()
        {
            IESPSearchManager manager = GetSearchManager();
            IESPSearchResult result = manager.Search(GetMockSearchRequest(), null);
            Assert.IsNotNull(result);
            // The dummy result has 10 results. check if that is true.
            // If it is ok the search should work
            Assert.AreEqual(10, result.Items.Count);
        }       


        /*
        [TestMethod]
        public void TestRemoveDomainFromUserName()
        {
            string result = ESPSearchManager_Accessor.RemovDomainFromUserName("ad\\username");
            Assert.AreEqual("username", result);

            result = ESPSearchManager_Accessor.RemovDomainFromUserName("\\username");
            Assert.AreEqual("username", result);            
        }
         */

        private SearchRequest GetMockSearchRequest()
        {
            HttpRequest httprequest = new HttpRequest("test.aspx", "http://test.com/test.aspx", "k=test");

            HttpResponse resp = new HttpResponse(null);
            HttpContext.Current = new HttpContext(httprequest, resp);
            SearchRequest searchRequest = new SearchRequest(ESPSearchGroupEnum.Group0, httprequest);
            
            searchRequest.NumberOfResultsPerPage = 10;
            searchRequest.ESPSearchView = "espsystemwebcluster";
            searchRequest.StaticCustomQueryProperties = "a=b";
            searchRequest.LemmatizationEnabled = "true";
            searchRequest.SynonymsEnabled = "true";
            searchRequest.Spellcheck = "auto";

            return searchRequest;
        }

        private IESPSearchManager GetSearchManager()
        {
            return ESPSearchManager_Accessor.GetInstance(CreateSearchFactory(), "MockFactory");
        }

        private ISearchFactory CreateSearchFactory()
        {
            Type temp_Class;
            temp_Class = Type.GetType("ESPWebPartsUnitTest.mock.ESPSearchFactoryMock");
            return (ISearchFactory)Activator.CreateInstance(temp_Class);
        }


    }
}
